<template>
    <div>
        <Tree :data="data1" class="tree" @on-select-change="onSelectChange"></Tree>
        <Button type="success" @click="onAdd">添加</Button>
        <Button type="error" @click="onDeletes">删除多个产品</Button>
        <Input v-model="filter.cateId" class="search">
        <Button slot="append" icon="ios-search" @click="onSearch"></Button>
        </Input>
        <Input v-model="filter.name" class="search1">
        <Button slot="append" icon="ios-search" @click="onSearch"></Button>
        </Input>
        <Table border :columns="columns" :data="filter.list"             @on-selection-change="onSelectionChange" class="table"></Table>
        <Page :total="filter.total" size="small" class="page"
                show-total show-elevator show-sizer
                @on-change="onChange"
                @on-page-size-change="onPageSizeChange">
        </Page>
        <Modal
                v-model="modal"
                title="数据操作">
            <Form ref="formValidate" :model="formValidate" :label-width="80">
                <FormItem label="产品名称" prop="name">
                    <Input v-model="formValidate.name" placeholder="请输入产品名称"></Input>
                </FormItem>
                <FormItem label="产品介绍" prop="content">
                    <Input v-model="formValidate.content" placeholder="请输入产品介绍"></Input>
                </FormItem>
                <FormItem label="产品价格" prop="price">
                    <Input v-model="formValidate.price" placeholder="请输入产品价格"></Input>
                </FormItem>
                <FormItem label="cateId" prop="cateId">
                    <Input v-model="formValidate.cateId"></Input>
                </FormItem>
                <FormItem label="时间" prop="date">
                    <Input v-model="formValidate.date"></Input>
                </FormItem>
                <FormItem>
                    <Button type="primary"
                            @click="handleSubmit('formValidate')">提交</Button>
                    <Button type="ghost"
                            @click="handleReset('formValidate')"
                            style="margin-left: 8px">重置</Button>
                </FormItem>
            </Form>
        </Modal>
    </div>
</template>

<script>
    import Base from '../../common/Base';
    export default {
        mixins : [Base],
        data () {
            return {
                content:'',
                title:'',
                module : 'product',
                formValidate : {
                    name : '',
                    content : '',
                    price : '',
                },
                columns : [
                    {
                        type : 'selection',
                        width : 60,
                        align : 'center'
                    },
                    {
                        title : 'cateId',
                        key : 'cateId',
                    },
                    {
                        title : '产品名称',
                        width: 140,
                        key : 'name'
                    },
                    {
                        title : '产品介绍',
                        width:200,
                        key : 'content'
                    },
                    {
                        title : '产品价格',
                        width:100,
                        key : 'price'
                    },
                    {
                        title : '操作',
                        key : 'action',
                        width : 200,
                        align : 'center',
                        render : (h, params) =>{
                            return h('div', [
                                h('Button', {
                                    props : {
                                        type : 'primary',
                                        size : 'small'
                                    },
                                    style : {
                                        marginRight : '5px'
                                    },
                                    on : {
                                        click : () =>{
                                            this.show(params.row)
                                        }
                                    }
                                }, '修改'),
                                h('Button', {
                                    props : {
                                        type : 'error',
                                        size : 'small'
                                    },
                                    on : {
                                        click : () =>{
                                            this.remove(params.row._id)
                                        }
                                    }
                                }, '删除'),
                            ]);
                        }
                    }
                ],
                data1: [],
            }
        },
        methods:{
            getTree(){
                this.$http.get('http://localhost:3000/cate/list/3')
                    .then(res=>{
                        console.log(res.data);
                        this.data1 = res.data;
                    })
            },
            onSelectChange(res){
                console.log(res);
                this.filter.cateId = res[0]._id;
                this.onSearch();

                this.formValidate.cateId = res[0]._id;
                //时间
                var d = new Date();
                var m1="/";
                var m2=":";
                var time=d.getFullYear()+m1+parseInt(d.getMonth()+1)+m1+d.getDate()+" "+d.getHours()+m2+d.getMinutes();
                console.log(time);
                this.formValidate.date = time;
            },
        },
        created(){
            this.getTree();
        }
    }
</script>
<style>
    .tree{
        width:148px;
        float:left;
    }
    .search{
        width:800px;
        display:none;
    }
    .search1{
        width:400px;
    }
    .table{
        width:800px;
        margin-left:148px;
    }
    .page{
        margin-left:148px;
    }
</style>